package com.example.demo.mybatisPlus;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.demo.mybatisPlus.entity.Dict;
import com.example.demo.mybatisPlus.entity.Product;
import com.example.demo.mybatisPlus.service.DictService;
import com.example.demo.mybatisPlus.service.ProductService;
import com.example.demo.utils.date.DateUtils;
import com.example.demo.utils.exception.BusinessException;
import com.example.demo.utils.json.JSONUtils;
import com.example.demo.utils.poi.ExcelUtil;
import com.example.demo.utils.returned.R;
import com.example.demo.utils.sql.UUID;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@AllArgsConstructor
public class MyBatisPlusController {

    private DictService dictService;
    private ProductService productService;

    @RequestMapping("/test/mybatis/plus")
    public String testMyBatisPlus() {
        /** 新增字典 */
        Dict dict = new Dict();
        dict.setId(UUID.getLongId(15));
        dict.setParentId(UUID.getLongId(15));
        dict.setDictValue("测试新增字典" + DateUtils.nowTime());
        System.out.println(dictService.save(dict) ? "插入数据成功" : "插入数据失败");
        /** 条件查询 */
        Dict sOne = dictService.getOne(Wrappers.query(dict));
        System.out.println("条件查询成功：" + JSONUtils.toJSONObject(sOne).toJSONString());
        /** 更改数据 */
        dict.setRemark("更改数据");
        System.out.println(dictService.updateById(dict) ? "更新数据成功" : "更新数据失败");
        /** 根据ID查询 */
        Dict sById = dictService.getById(dict.getId());
        System.out.println("主键查询成功：" + JSONUtils.toJSONObject(sById).toJSONString());

        /** 根据数据库的id自行赋值 */
        /** 根据条件删除 */
        System.out.println(dictService.remove(Wrappers.query(dict)) ? "条件删除成功" : "条件删除失败");
        /** 根据主键ID删除 */
        System.out.println(dictService.removeById(118888702071838L) ? "主键删除成功" : "主键删除失败");
        /** 删除多条 */
        System.out.println(dictService.removeByIds(Arrays.asList(139899314432244L, 453384986314113L)) ? "删除多条成功" : "删除多条失败");

        return "Ok";
    }

    /** 测试poi导出 */
    @RequestMapping("/test/poi")
    public R testPoi() {
        ExcelUtil<Product> excelUtil = new ExcelUtil<>(Product.class);
        return excelUtil.exportExcel(productService.list(), "商品列表");
    }

}
